package com.fare.taluo.web.action;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.fare.taluo.BaseFdcAction;
import com.fare.taluo.application.crawl.AdapterUploadedApplication;
import com.fare.taluo.vo.core.AdapterUploadedEntityVo;
/**
 * @param args
 */
@Controller("adapterUploadedAction")
@Scope("prototype")
public class AdapterUploadedAction extends BaseFdcAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 3253409823517648766L;
	@Resource(name = "adapterUploadedApplication")
	private AdapterUploadedApplication adapterUploadedApplication;
	
	public AdapterUploadedApplication getAdapterUploadedApplication() {
		return adapterUploadedApplication;
	}

	private String classFileName;//上传文件名
	private File fileData;//上传的文件
	private String fileName;
    private String stats;
    private String filePath;
    private String tShow;
    private FileOutputStream fos = null;
    FileItem tmpItem = null;
    private ServletContext context ;
	public String getStats() {
		return stats;
	}

	public String getFilePath() {
		return filePath;
	}
	

	public String gettShow() {
		return tShow;
	}

	public void settShow(String tShow) {
		this.tShow = tShow;
	}

	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}


	public void setStats(String stats) {
		this.stats = stats;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getClassFileName() {
		return classFileName;
	}

	public void setClassFileName(String classFileName) {
		this.classFileName = classFileName;
	}

	public File getFileData() {
		return fileData;
	}

	public void setFileData(File fileData) {
		this.fileData = fileData;
	}

	public void setAdapterUploadedApplication(
			AdapterUploadedApplication adapterUploadedApplication) {
		this.adapterUploadedApplication = adapterUploadedApplication;
	}

	
	
	public ServletContext getContext() {
		return context;
	}


	public void setContext(ServletContext context) {
		this.context = context;
	}


	//上传文件并保存
	public void SaveAdapterUploaded() throws IOException{
		InputStream inputStream = null;
		File file = null;
		try {
			AdapterUploadedEntityVo vo = new AdapterUploadedEntityVo();
			String root=ServletActionContext.getRequest().getRealPath("/upload");
			System.out.println("fielPath:"+filePath);
			System.out.println("root:"+root);
			String saveFileName = null;
			if(this.fileName != null && this.fileName.contains("\\")){
			  int lastIndex = this.fileName.lastIndexOf("\\");
			  saveFileName = this.fileName.substring(lastIndex);
			}else{
				saveFileName = this.fileName;
			}
			fos = new FileOutputStream(root+"////"+ new String((saveFileName).getBytes("ISO-8859-1"),"UTF-8"));
			 file = new File(new String((saveFileName).getBytes("ISO-8859-1"),"UTF-8"));	
			System.out.println("++fos++"+fos);
			int a = this.fileName.lastIndexOf(".");
			String str = this.fileName.substring(a+1,this.fileName.length());
			inputStream = ServletActionContext.getRequest().getInputStream();
			System.out.println("str:"+str);
			if(!StringUtils.equalsIgnoreCase(str, "class")){
				ServletActionContext.getResponse().getWriter().print("适配器文件格式错误"); 
//				return null;
			}
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte [] buffer = new byte[1024];
			int length = 0 ;
			for(int n;(n=inputStream.read(buffer))!=-1;){
				//baos.write(buffer, 0, length);
				fos.write(buffer, 0, length);
			}
			if (inputStream.available() > 1024 * 1024 * 1) {
				file.delete();    
				ServletActionContext.getResponse().getWriter().print("文件不能大于1M"); 
//				return null ;
			}
			
			vo.setClassName(file.getName());
			vo.setAdapterData(buffer);
			vo.setClassSize(file.length());
			vo.setClassSize(file.length());
			System.out.println("++++++"+file.length());
			System.out.println("+++++2+++"+baos.size());
			System.out.println("+++++3+++"+inputStream.available());
//			 String size = Integer.toString(file.getFileSize()); 
//			uploadBeanForm.setSize(size); 
			Long bon = adapterUploadedApplication.SaveAdapterUploaded(vo);
			System.out.println("打印上传后的数据++"+bon);
			if(bon>0){
				System.out.println("打印上传后的数据++"+bon);
				String bonStr=bon.toString();
				this.stats =bonStr;
			//	request.setAttribute("stats", stats);
			//	this.stats="上传成功!";
				ServletActionContext.getResponse().getWriter().print(this.stats);
//				return bon;
			}else{
				this.tShow="上传失败";
				ServletActionContext.getResponse().getWriter().print(this.tShow);
//				return -1L;
			}
			
			//outputStream.flush();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			this.tShow="上传失败!"+e.getMessage();
			ServletActionContext.getResponse().getWriter().print(this.tShow);
			e.printStackTrace();
		} 
	//	return -1L;
	}
}
